<?php
/**
 * 小程序接口定义
 *
 * @author alluu
 * @url
 */
function parse_headers(){
    $headers = [];
    foreach ($_SERVER as $name => $value)
    {
        if (substr($name, 0, 5) == 'HTTP_')
        {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    return $headers;
}
define('IN_MOBILE', true);
require '../../framework/bootstrap.inc.php';
$data = json_decode(html_entity_decode($_POST['bizContent']), true);
file_put_contents('fdd_notify_'.date('Ym').'.txt', print_r($data, true).PHP_EOL,FILE_APPEND);
file_put_contents('fdd_notify_'.date('Ym').'.txt', print_r(parse_headers(), true).PHP_EOL,FILE_APPEND);
if(!$data){
    file_put_contents('fdd_notify_'.date('Y-m-d').'.txt', '回调错误：数据不存在 '.PHP_EOL,FILE_APPEND);
    exit(json_encode(['msg'=>'fail']));
}

$header = parse_headers();

if ($header['X-Fasc-App-Id'] != '80000490') {
    file_put_contents('fdd_notify_'.date('Y-m-d').'.txt', '回调错误：appid不正确 '.PHP_EOL,FILE_APPEND);
    exit(json_encode(['msg'=>'fail']));
}

$waitHandleEvent = ['sign-task-signed', 'sign-task-joined'];
if (!in_array($header['X-Fasc-Event'], $waitHandleEvent)) {
    exit(json_encode(['msg'=>'success']));
}

if ($header['X-Fasc-Event'] == 'sign-task-signed'){
    $order = pdo_update('yzzc_sun_order',['fdd_status'=>2],['fdd_task_id'=>$data['signTaskId']]);
    if (!$order) {
        file_put_contents('fdd_notify_'.date('Y-m-d').'.txt', "回调错误：订单更新失败{$data['signTaskId']} ".PHP_EOL,FILE_APPEND);
        exit(json_encode(['msg'=>'fail']));
    }
}
exit(json_encode(['msg'=>'success']));

